<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:rsm="urn:un:unece:uncefact:data:draft:Test:1">
	
	<xsl:output encoding="UTF-8"  indent="yes" />
	
	<xsl:template match="/">
		<DespatchAdvice xmlns="urn:un:unece:uncefact:data:draft:Test:1"
			xmlns:ram="urn:un:unece:uncefact:data:draft:ReusableAggregateBusinessInformationEntity:1"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			xsi:schemaLocation="urn:un:unece:uncefact:data:draft:Test:1 FoodserviceDespatchAdvice.xsd">
			<Identifier><xsl:call-template name="GetDespatchAdviceIdentifier" /></Identifier>
			<OrderReference>
				<xsl:value-of select="/rsm:FoodservicePurchaseOrder/rsm:Identifier" />
			</OrderReference>
			<IssueDate>
				<xsl:value-of select="/rsm:FoodservicePurchaseOrder/rsm:IssueDate" />
			</IssueDate>
			<DeliveryDate>
				<xsl:value-of select="/rsm:FoodservicePurchaseOrder/rsm:DeliveryDate" />
			</DeliveryDate>
			<xsl:sequence select="/rsm:FoodservicePurchaseOrder/rsm:Buyer" />
			<xsl:sequence select="/rsm:FoodservicePurchaseOrder/rsm:Seller" />

			<xsl:sequence select="/rsm:FoodservicePurchaseOrder/rsm:ShipTo" />
			
			<xsl:for-each select="/rsm:FoodservicePurchaseOrder/rsm:Item">
				<xsl:variable name="prod.id" select="ram:ProductID"/>
				<xsl:if test="ram:OrderItemID != '' ">
				<Item>
					<ram:DespatchItemID>
						<xsl:value-of select="ram:OrderItemID" />
					</ram:DespatchItemID>
					<ram:DeliveredQuantity>
						<xsl:value-of select="ram:Quantity" />
					</ram:DeliveredQuantity>
					<xsl:sequence select="$prod.id" />
					<ram:BatchID>1</ram:BatchID>
					<ram:BestBeforeDate>2007-10-31</ram:BestBeforeDate>
				</Item>
				</xsl:if>
			</xsl:for-each>
		</DespatchAdvice>
	</xsl:template>
	
	<xsl:template name="GetInvoiceIdentifier">
		<xsl:variable name="po.id" select="/rsm:FoodservicePurchaseOrder/rsm:Identifier" />
		
		<xsl:sequence select="concat( 'FSI-', $po.id )" />
	</xsl:template>
	
	<xsl:template name="GetDespatchAdviceIdentifier">
		<xsl:variable name="po.id" select="/rsm:FoodservicePurchaseOrder/rsm:Identifier" />
		
		<xsl:sequence select="concat( 'FSDA-', $po.id )" />
	</xsl:template>
</xsl:stylesheet>
